/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-01-09 09:13:31
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-30 17:55:27
 * @FilePath: /yt-devops/lib/model/data_type_config.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:json_annotation/json_annotation.dart';
import 'package:yt_devops/model/device_obj/properties_model.dart';
import 'package:yt_devops/model/device_obj/spec/double_spec.dart';
import 'package:yt_devops/model/device_obj/spec/time_range_spec.dart';
import 'device_obj/spec/arrays_spec.dart';
part 'data_type_config.g.dart';
@JsonSerializable()
class DataTypeConfig {
  DataTypeConfigEnum type;
  late Object specs;
  DataTypeConfig({required this.type, Object? spec}) {
    if (type == DataTypeConfigEnum.double ||
        type == DataTypeConfigEnum.long ||
        type == DataTypeConfigEnum.int) {
      specs = DoubleSpec.fromJson(spec as Map<String, dynamic>);
    } else if (type == DataTypeConfigEnum.timeRange) {
      specs = TimeRageSpec.fromJson(spec as Map<String, dynamic>);
    } else if (type == DataTypeConfigEnum.array) {
      specs = ArraysSpec.fromJson(spec as Map<String, dynamic>);
    } else if (type == DataTypeConfigEnum.struct) {
      specs = spec == null
          ? []
          : List.generate(
              (spec as List).length,
              (index) => PropertiesModel.fromJson(
                  spec[index] as Map<String, dynamic>));
    } else {
      specs = spec!;
    }
  }
  factory DataTypeConfig.fromJson(Map<String, dynamic> json) =>
      _$DataTypeConfigFromJson(json);

  Map<String, dynamic> toJson() => _$DataTypeConfigToJson(this);
}

@JsonEnum(valueField: 'label')
enum DataTypeConfigEnum {
  double('double'),
  struct('struct'),
  array('array'),
  enumType('enum'),
  string('string'),
  bigString('String'),
  timeRange('timeRange'),
  boolean('boolean'),
  int('int'),
  timestamp('timestamp'),
  treeSelect('treeSelect'),
  long('long');
  final String label;
  const DataTypeConfigEnum(this.label);
}
